package com.formiaomiao.demain.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.formiaomiao.demain.R;
import com.formiaomiao.demain.base.BaseActivity;
import com.formiaomiao.demain.base.BaseApplication;
import com.formiaomiao.demain.base.BaseUrl;
import com.formiaomiao.demain.db.IsPushBean;
import com.formiaomiao.demain.db.MsgBean;
import com.formiaomiao.demain.greendao.IsPushBeanDao;
import com.formiaomiao.demain.greendao.MsgBeanDao;
import com.formiaomiao.demain.util.MessageEvent;
import com.formiaomiao.demain.util.PreferenceUtils;
import com.formiaomiao.demain.util.PreferenceValues;
import com.formiaomiao.demain.util.StaticValues;
import com.formiaomiao.demain.util.ToastUtils;
import com.formiaomiao.demain.util.UseGlideUtils;
import com.hyphenate.chat.EMClient;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ch.ielse.view.SwitchView;

public class ChatSettingsActivity extends BaseActivity {

    @BindView(R.id.iv_class_back)
    ImageView ivClassBack;
    @BindView(R.id.tv_class_name)
    TextView tvClassName;
    @BindView(R.id.iv_chat_head)
    ImageView ivChatHead;
    @BindView(R.id.tv_chat_name)
    TextView tvChatName;
    @BindView(R.id.sw_chat_msg)
    SwitchView swChatMsg;
    @BindView(R.id.tv_chat_background)
    TextView tvChatBackground;
    @BindView(R.id.tv_chat_delete)
    TextView tvChatDelete;
    @BindView(R.id.tv_chat_complaint)
    TextView tvChatComplaint;

    MsgBeanDao msgBeanDao;
    IsPushBeanDao isPushBeanDao;
    IsPushBean pushBean;

    private static ChatSettingsActivity chatSettingsActivity;

    public static ChatSettingsActivity getInstances() {
        return chatSettingsActivity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_settings);
        ButterKnife.bind(this);
        ViewInit();
    }

    private void ViewInit() {
        chatSettingsActivity = this;
        tvClassName.setText("聊天信息");
        UseGlideUtils.setGlide(this, getIntent().getStringExtra("userhead"), ivChatHead);
        tvChatName.setText(getIntent().getStringExtra("friendremarks") == null ||
                getIntent().getStringExtra("friendremarks").equals("") ?
                getIntent().getStringExtra("nickname") :
                getIntent().getStringExtra("friendremarks"));
        msgBeanDao = BaseApplication.getInstances().getDaoSession().getMsgBeanDao();
        isPushBeanDao = BaseApplication.getInstances().getDaoSession().getIsPushBeanDao();
        pushBean = new IsPushBean();
        try {
            pushBean = isPushBeanDao.queryBuilder().where(IsPushBeanDao.Properties.ChatMsgId.
                    eq(StaticValues.username + "#" +
                            getIntent().getStringExtra("username"))).unique();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (isPushBeanDao == null) {
            swChatMsg.setOpened(false);
        } else if (pushBean == null) {
            swChatMsg.setOpened(false);
        } else if (pushBean.getIspush()) {
            swChatMsg.setOpened(true);
        } else {
            swChatMsg.setOpened(false);
        }
        swChatMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isOpened = swChatMsg.isOpened();
                pushBean = new IsPushBean();
                pushBean.setId(null);
                pushBean.setChatMsgId(StaticValues.username + "#" +
                        getIntent().getStringExtra("username"));
                if (isOpened) {
                    pushBean.setIspush(true);
                } else {
                    pushBean.setIspush(false);
                }
                if (isPushBeanDao.queryBuilder().where(IsPushBeanDao.Properties.ChatMsgId.
                        eq(StaticValues.username + "#" +
                                getIntent().getStringExtra("username"))).unique() == null) {
                    isPushBeanDao.insert(pushBean);
                } else {
                    pushBean = isPushBeanDao.queryBuilder().where(IsPushBeanDao.Properties.ChatMsgId.
                            eq(StaticValues.username + "#" +
                                    getIntent().getStringExtra("username"))).unique();
                    pushBean.setIspush(isOpened);
                    isPushBeanDao.update(pushBean);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @OnClick({R.id.iv_class_back, R.id.iv_chat_head, R.id.tv_chat_background, R.id.tv_chat_delete, R.id.tv_chat_complaint})
    public void onViewClicked(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.iv_class_back:
                finish();
                break;
            case R.id.iv_chat_head:
                intent.putExtra("id", getIntent().getIntExtra("id", 0));
                intent.putExtra("nickname", getIntent().getStringExtra("nickname"));
                intent.putExtra("userhead", getIntent().getStringExtra("userhead"));
                intent.putExtra("username", getIntent().getStringExtra("username"));
                intent.putExtra("friendremarks", getIntent().getStringExtra("friendremarks") == null ||
                        getIntent().getStringExtra("friendremarks").equals("") ?
                        getIntent().getStringExtra("nickname") :
                        getIntent().getStringExtra("friendremarks"));
                intent.setClass(ChatSettingsActivity.this, FriendDetailActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_chat_background:
                intent.putExtra("username", getIntent().getStringExtra("username"));
                intent.setClass(ChatSettingsActivity.this, ReplaceBackgroundActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_chat_delete:
                new AlertDialog.Builder(this)
                        .setMessage("是否删除聊天记录")
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //删除和某个user会话，如果需要保留聊天记录，传false
                        EMClient.getInstance().chatManager().deleteConversation(getIntent().getStringExtra("username"), true);
                        List<MsgBean> msgBeans = msgBeanDao.loadAll();
                        for (int j = 0; j < msgBeans.size(); j++) {
                            if (msgBeans.get(j).getChatMsgId().equals(StaticValues.username + "#" + getIntent().getStringExtra("username"))) {
                                msgBeanDao.deleteByKey(msgBeans.get(j).getId());
                            }
                        }
                        dialog.dismiss();
                        EventBus.getDefault().post(new MessageEvent("删除聊天记录," + StaticValues.username + "#" + getIntent().getStringExtra("username")));
                    }
                }).show();
                break;
            case R.id.tv_chat_complaint:
                intent.setClass(this, ComplaintFriendActivity.class);
                intent.putExtra("id", getIntent().getIntExtra("id", 0));
                startActivity(intent);
                break;
        }
    }
}
